/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.util.ArrayList;
import com.jme3.scene.Node;


/**
 * Interactive in-game screen of the palace of the caliph.
 */
public class PalaceOfTheCaliphInterface extends Node {
	
	
	/**
	 * List of all registered mouseOver event elements of the palace of the caliph.
	 */
	private ArrayList<IMouseOverable> mouseOverables = new ArrayList<IMouseOverable>();
	
	
	/**
	 * List of all registered click event elements of the palace of the caliph.
	 */
	private ArrayList<IClickable> clickables = new ArrayList<IClickable>();
	
	
	/**
	 * Geometry node of all content of the window.
	 */
	private Node palaceOfTheCaliph;
	
	
	/**
	 * Text telling player who the current protege of the caliph is.
	 */
	private GuiText currentProtege;
	
	
	/**
	 * Color constraint flag of the palace of the caliph
	 */
	private GuiQuad colorConstraintIcon;
	
	
	/**
	 * Text telling player that it's not his turn
	 */
	private GuiText notYourTurn;

	
	/**
	 * Become protege of the caliph button
	 */
	private GuiButton becomeProtege;
	
	
	/**
	 * Close window button
	 */
	private GuiButton close;
	
	
	/**
	 * Parent node of all buying agents in the palace of the caliph.
	 */
	private Node buyingAgentsNode;
	
	
	/**
	 * Window where the player can choose which buying agent he wants to use.
	 */
	private BuyingAgentInterface buyingAgentInterface;
	
	
	/**
	 * Constructor: Creates geometry.
	 */
	public PalaceOfTheCaliphInterface() {
		
		palaceOfTheCaliph = new Node();
		
		GuiWindow window = new GuiWindow(GUI.Offset.SCENE, 29, 30, 710, 610, 50);
		palaceOfTheCaliph.attachChild(window);
		
		GuiText title = new GuiText("Palast des Kalifen", Fonts.getLarge());
		title.setPositionSize(GUI.Offset.SCENE, (768 - (int)title.getLineWidth()) / 2, 60, 51);
		palaceOfTheCaliph.attachChild(title);
		
		currentProtege = new GuiText("");
		palaceOfTheCaliph.attachChild(currentProtege);
		
		colorConstraintIcon = new GuiQuad();
		colorConstraintIcon.setPositionSize(GUI.Offset.SCENE, 349, 265, 80, 30, 51);
		colorConstraintIcon.setMaterial(MaterialManager.getColorConstraintFlags(), 0.0f, 0.0f, 1.0f, 0.2f);
		palaceOfTheCaliph.attachChild(colorConstraintIcon.create());
		
		GuiSlot slot = new GuiSlot(GUI.Offset.SCENE, 364, 335, 40, 75, 51);
		palaceOfTheCaliph.attachChild(slot);

		buyingAgentsNode = new Node();
		palaceOfTheCaliph.attachChild(buyingAgentsNode);
		
		becomeProtege = new GuiButton("Prot\u00E9g\u00E9 des Kalifen werden");
		becomeProtege.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 430, 300, 51);
		becomeProtege.setAction(new ICallback() { 
			public void run() {
				becomeProtege.setActive(false);
				close.setActive(false);
				GUI.unregisterMouseOverable(mouseOverables);
				GUI.unregisterClickable(clickables);
				buyingAgentInterface = new BuyingAgentInterface(false, BuyingAgent.Color.NONE);
				buyingAgentInterface.setCancelAction(new ICallback() { 
					public void run() {
						becomeProtege.setActive(true);
						close.setActive(true);
						GUI.registerMouseOverable(mouseOverables);
						GUI.registerClickable(clickables);
					}
				});
				buyingAgentInterface.setCarryOutActionAction(new ICallback() {
					public void run() {
						becomeProtege.setActive(true);
						close.setActive(true);
						GUI.registerMouseOverable(mouseOverables);
						GUI.registerClickable(clickables);
						int[] buyingAgentIDs = buyingAgentInterface.getBuyingAgents();
						
						boolean success = Game.board().getPalaceOfTheCaliph().doAction(Game.board().getMyIndex(), buyingAgentIDs[0], buyingAgentIDs[1]);
						if(!success) {
							System.err.println("Error while executing palace of the caliph action.");
							return;
						}
						GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
					}
				});
			}
		});
		becomeProtege.create();
		
		notYourTurn = new GuiText("Du bist nicht am Zug.");
		notYourTurn.setPositionSize(GUI.Offset.SCENE, (768 - (int)notYourTurn.getLineWidth()) / 2, 470, 51);
		
		close = new GuiButton("Schliessen");
		close.setPositionSize(GUI.Offset.SCENE, (768 - 160) / 2, 555, 160, 51);
		close.setAction(new ICallback() { 
			public void run() { 
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		palaceOfTheCaliph.attachChild(close.create());
		
		clickables.add(becomeProtege);
		clickables.add(close);
		
		mouseOverables.add(becomeProtege);
		mouseOverables.add(close);
		
	}
	
	
	/**
	 * Attach all needed geometry and register all mouseOver and click events.
	 */
	public void show() {
		Scenery.startCamAnimation(Scenery.Location.PALACE_OF_THE_CALIPH, new ICallback() {
			public void run() {
				GUI.getPalaceOfTheCaliph().attachChild(palaceOfTheCaliph);
				GUI.registerMouseOverable(mouseOverables);
				GUI.registerClickable(clickables);
				
				GUI.getPalaceOfTheCaliph().updatePalaceOfTheCaliph();
			}
		});
	}
	
	
	/**
	 * Detach all geometry and unregister all mouseOver and click events.
	 */
	public void hide() {
		this.detachChild(palaceOfTheCaliph);
		GUI.unregisterMouseOverable(mouseOverables);
		GUI.unregisterClickable(clickables);
		Scenery.startCamAnimation(Scenery.Location.BASIC_SCENE_CAMERA, null);
	}


	/**
	 * Updates palace of the caliph when the server sends a update message.
	 */
	public void updatePalaceOfTheCaliph() {
		
		int protege = Game.board().getProtege();
		
		if(protege == Game.board().getMyIndex()) {
			currentProtege.setText("Zurzeit bist du der Prot\u00E9g\u00E9 des Kalifen.");
		} else {
			currentProtege.setText("Zurzeit ist " + Game.board().getPlayer(protege).getName() + " der Prot\u00E9g\u00E9 des Kalifen.");
		}
		currentProtege.setPositionSize(GUI.Offset.SCENE, (768 - (int)currentProtege.getLineWidth()) / 2, 170, 51);
		
		BuyingAgent.Color colorConstraint = Game.board().getPalaceOfTheCaliph().getColorConstraint();
		if(colorConstraint == BuyingAgent.Color.NONE && palaceOfTheCaliph.hasChild(colorConstraintIcon)) {
			palaceOfTheCaliph.detachChild(colorConstraintIcon);
		}
		if(colorConstraint != BuyingAgent.Color.NONE && !palaceOfTheCaliph.hasChild(colorConstraintIcon)) {
			palaceOfTheCaliph.attachChild(colorConstraintIcon);
		}
		if(colorConstraint == BuyingAgent.Color.BLUE) {
			colorConstraintIcon.changeTextureCoords(0.0f, 0.0f, 1.0f, 0.2f);
		} else if(colorConstraint == BuyingAgent.Color.GREEN) {
			colorConstraintIcon.changeTextureCoords(0.0f, 0.2f, 1.0f, 0.2f);
		} else if(colorConstraint == BuyingAgent.Color.ORANGE) {
			colorConstraintIcon.changeTextureCoords(0.0f, 0.4f, 1.0f, 0.2f);
		} else if(colorConstraint == BuyingAgent.Color.PURPLE) {
			colorConstraintIcon.changeTextureCoords(0.0f, 0.6f, 1.0f, 0.2f);
		} else if(colorConstraint == BuyingAgent.Color.YELLOW) {
			colorConstraintIcon.changeTextureCoords(0.0f, 0.8f, 1.0f, 0.2f);
		}
		
		int[] useOfSlots = Game.board().getPalaceOfTheCaliph().getBuyingAgentSlots();
		buyingAgentsNode.detachAllChildren();
		
		if(useOfSlots[0] == 1) {
			buyingAgentsNode.attachChild(GuiBuyingAgent.getColor2D(colorConstraint, GUI.Offset.SCENE, 364, 335, 52));
		} else if(useOfSlots[0] == 2) {
			buyingAgentsNode.attachChild(GuiBuyingAgent.getColor2D(BuyingAgent.Color.NONE, GUI.Offset.SCENE, 364, 335, 52));
		}
		
		becomeProtege.setActive(false);
		
		if(Game.board().getTurn() == Game.board().getMyIndex()) {
			if(palaceOfTheCaliph.hasChild(notYourTurn)) {
				palaceOfTheCaliph.detachChild(notYourTurn);
			}
			if(!palaceOfTheCaliph.hasChild(becomeProtege)) {
				palaceOfTheCaliph.attachChild(becomeProtege);
			}
			if(useOfSlots[0] == 0) {
				becomeProtege.setActive(true);
			}
		} else {
			if(!palaceOfTheCaliph.hasChild(notYourTurn)) {
				palaceOfTheCaliph.attachChild(notYourTurn);
			}
			if(palaceOfTheCaliph.hasChild(becomeProtege)) {
				palaceOfTheCaliph.detachChild(becomeProtege);
			}
		}
	}
	
}
